package cn.msqweb.controller;

import cn.msqweb.entity.SysUser;
import cn.msqweb.model.Result;
import cn.msqweb.service.SysUserService;
import cn.msqweb.util.AuthUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 *  系统管理员控制层
 */

@Api(tags = "系统管理员接口管理")
@RequestMapping("sys/user")
@RestController
public class SysUserController {

    @Resource
    private SysUserService sysUserService;


    /**
     * 通过ID查询当前用户
     * @return
     */
    @GetMapping("info")
    public Result<SysUser> loadSysUserInfo(){
        Long loginSecurityUserID = AuthUtils.getLoginSecurityUserID();
        SysUser sysUser = sysUserService.getById(loginSecurityUserID);
        sysUser.setPassword("");
        return Result.success(sysUser);
    }

    @ApiOperation("多条件分页查询管理员列表")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('sys:user:page')") //账户权限验证
    public Result<Page<SysUser>> loadSysUserPage(@ApiParam("页码") @RequestParam Long current,
                                                 @ApiParam("每页显示条数") @RequestParam Long size,
                                                 @ApiParam("管理员名称") @RequestParam(required = false) String username
                                                 ){
        Page<SysUser> sysUserPage = new Page<>(current,size);
        Page<SysUser> page = sysUserService.page(sysUserPage, new LambdaQueryWrapper<SysUser>()
                .like(StringUtils.hasText(username), SysUser::getUsername, username)
                .orderByDesc(SysUser::getCreateTime)
        );

        return Result.success(page);
    }

    @ApiOperation("新增管理员")
    @PostMapping()
    @PreAuthorize("hasAuthority('sys:user:save')")
    public Result<String> saveSysUser(@ApiParam("管理员对象") @RequestBody SysUser sysUser){
        Boolean saved = sysUserService.saveSysUser(sysUser);
        return Result.handler(saved);
    }

    @ApiOperation("根据标识查询管理员详情信息")
    @GetMapping("info/{userId}")
    @PreAuthorize("hasAuthority('sys:user:info')")
    public Result<SysUser> loadSysUserDetail(@ApiParam("管理员标识") @PathVariable Long userId) {
        SysUser sysUser = sysUserService.querySysUserDetailByUserId(userId);
        return Result.success(sysUser);
    }

    @ApiOperation("修改管理员信息")
    @PutMapping
    @PreAuthorize("hasAuthority('sys:user:update')")
    public Result<String> modifySysUser(@ApiParam("管理员对象") @RequestBody SysUser sysUser) {
        Boolean modified = sysUserService.modifySysUser(sysUser);
        return Result.handler(modified);
    }

    @ApiOperation("批量删除管理员")
    @DeleteMapping("{userIds}")
    @PreAuthorize("hasAuthority('sys:user:delete')")
    public Result<String> removeSysUser(@ApiParam("用户id集合") @PathVariable List<Long> userIds) {
        Boolean removed = sysUserService.removeSysUserByUserIds(userIds);
        return Result.handler(removed);
    }
}
